package com.neutech.maplestory.client;
import com.neutech.maplestory.entity.*;
import com.neutech.maplestory.util.ImageUtil;
import com.neutech.maplestory.util.MapleStoryFrame;
import com.neutech.maplestory.util.MyMusicThread;

import javax.swing.plaf.PanelUI;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 游戏客户端
 */
public class MapleStoryClient extends MapleStoryFrame {

    public Hero hero = new Hero(this,50,500);
    public List<Arrow> arrows = new CopyOnWriteArrayList<>();
    public Ground bg = new Ground(this,-800, 0,"bg1");

    public List <Mob> mobs = new CopyOnWriteArrayList<>();
    public List<Item> items = new CopyOnWriteArrayList<>();
    public List<Power> powers = new CopyOnWriteArrayList<>();

    public Random r = new Random();

    public List<Ground> grounds = new CopyOnWriteArrayList<>();

    public ItemPackage itemPackage = new ItemPackage(this);
    public List<Ladder> ladders = new CopyOnWriteArrayList<>();

    {
        Ladder ladder01 = new Ladder(this,995,530,50,260);
        ladders.add(ladder01);
        Ladder ladder02 = new Ladder(this,1372,428,15,365);
        ladders.add(ladder02);
        Ladder ladder03 = new Ladder(this,430,540,50,130);
        ladders.add(ladder03);
    }
    {
        for (int i = 0; i < r.nextInt(10 -5)+8; i++) {
            Mob mob = new Mob(this,700 + (r.nextInt(800)),740,5);
            mobs.add(mob);
        }
    }
    {
        Ground ground01 = new Ground(this,0,730,530,270);
        grounds.add(ground01);
        Ground ground02 = new Ground(this,530,790,1370,210);
        grounds.add(ground02);
        Ground ground03 = new Ground(this,730,730,80,10);
        grounds.add(ground03);
        Ground ground04 = new Ground(this,615,690,80,10);
        grounds.add(ground04);
        Ground ground05 = new Ground(this,755,640,80,10);
        grounds.add(ground05);
        Ground ground06 = new Ground(this,855,590,80,10);
        grounds.add(ground06);
        Ground ground07 = new Ground(this,930,530,150,10);
        grounds.add(ground07);
        Ground ground08 = new Ground(this,1100,480,80,10);
        grounds.add(ground08);
        Ground ground09 = new Ground(this,1210,430,480,10);
        grounds.add(ground09);
        Ground ground10 = new Ground(this,340,530,145,10);
        grounds.add(ground10);
    }

    @Override
    public void init() {
        super.init();
        //添加键盘监听器
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                hero.keyPressed(e);
            }
            @Override
            public void keyReleased(KeyEvent e) {
                hero.keyReleased(e);
                if (e.getKeyCode() == KeyEvent.VK_I){
                    //开关切换
                    itemPackage.live = !itemPackage.live;
                }

            }
        });
    }
/*
    public Image img = ImageUtil.getImage("bg1");
    public int i =1;
    public int bgCount =0;
*/

    @Override
    public void paint(Graphics g) {

        Iterator<Ladder> itLadders = ladders.iterator();
        while (itLadders.hasNext()) {
            Ladder ladder = itLadders.next();
            ladder.draw(g);
        }
        bg.bg1(g);
        Iterator<Ground> itGrounds = grounds.iterator();
        while (itGrounds.hasNext()){
            Ground ground = itGrounds.next();
            ground.draw(g);
        }
        hero.draw(g);

        //是用迭代器和CopyWriteArrayList解决数组索引移动(非线程安全)
        Iterator<Arrow> itArrows = arrows.iterator();
        while (itArrows.hasNext()){
            Arrow arrow = itArrows.next();
            arrow.draw(g);
            arrow.hit(mobs);
        }
        Iterator<Power> itPowers = powers.iterator();
        while (itPowers.hasNext()){
            Power power = itPowers.next();
            power.draw(g);
        }

        /*for (int i = 0; i < arrows.size(); i++) {
            Arrow arrow = arrows.get(i);
            arrow.draw(g);
            arrow.hit(mobs);
        }*/
       Iterator<Mob> itMobs = mobs.iterator();
        while (itMobs.hasNext()){
            Mob mob = itMobs.next();
            mob.draw(g);
        }
        Iterator<Item> itItems = items.iterator();
        while (itItems.hasNext()){
            Item item = itItems.next();
            item.draw(g);
        }
        itemPackage.draw(g);



        Font f = g.getFont();
        g.setFont(new Font("幼圆",Font.BOLD,30));
        g.drawString("人物方向为" + hero.dir,100,100);
        g.drawString("人物的动作为" + hero.action,100,150);
        g.drawString("人物的MP" + hero.MP,100,200);
        g.drawString("人物的HP" + hero.HP,100,250);
        g.drawString("经验值" + hero.exp,100,300);
        g.setFont(f);
    /*    for (;;){
            g.drawImage(img,0+ i *533,0,null);
        }*/

    }
    public static void main(String[] args) {
        new MapleStoryClient().init();
        new MyMusicThread("bgm.mp3", true).start();

    }

/*    public BG bg = new BG();
    class BG extends AbstractMapleObject{
        public Image img ;
        public int i =0;
        public BG(){
            this.img = ImageUtil.getImage("bg1");
            this.i = i;
        }

        @Override
        public void draw(Graphics g) {

            for (int i1 = 0; i1 < 10; i1++) {
                g.drawImage(img,x + 535 *i,0,null);
                i++;
            }
            move();

        }
        @Override
        public void move() {
            this.x +=10;
        }
    }*/
}
